package com.wardrobe.soa.backend.goods;

import com.google.common.collect.Maps;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.goods.GoodsBean;
import com.wardrobe.domain.goods.GoodsFromBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.goods_classify.GoodsClassifyBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/16.
 */
@RestController
@RequestMapping("/backend/goods")
public class BackGoodsController {
    private static final Logger logger = LoggerFactory.getLogger(BackGoodsController.class);

    @Autowired
    private BackGoodsService backGoodsService;

    @RequestMapping("/getGoodsList")
    public Map<String, Object> getGoodsList(GoodsFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        List<GoodsShowBean> list = backGoodsService.getGoodsList(fromBean);
        list.stream().forEach(goodsClassifyBean -> {
            goodsClassifyBean.setCreateTime(goodsClassifyBean.getCreateTime().substring(0,goodsClassifyBean.getCreateTime().length()-2));
            goodsClassifyBean.setUpdateTime(goodsClassifyBean.getUpdateTime().substring(0,goodsClassifyBean.getUpdateTime().length()-2));
            goodsClassifyBean.setShowPicture(goodsClassifyBean.getShowPicture().split(";")[0]);
        });
        long count = backGoodsService.countGoods(fromBean);
        int pageCount = (int) Math.ceil(((double) count / fromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", fromBean.getPageSize());
        return result;
    }

    @RequestMapping("/saveGoods")
    public Map<String,Object> saveGoods(GoodsBean goodsBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            int status = backGoodsService.saveGoods(goodsBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","添加成功");
            }else{
                result.put("status",2);
                result.put("msg","添加失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","添加失败");
            logger.error("saveGoods error",e);
        }
        return result;
    }


    @RequestMapping("/modifyGoods")
    public Map<String,Object> modifyGoods(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backGoodsService.modifyGoods(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyGoods error",e);
        }
        return result;
    }
}
